package model.dao;

import java.util.ArrayList;
import java.util.List;
import model.dao.ulti.HibernateUtil;
import model.pojo.*;
import org.hibernate.Query;
import org.hibernate.Session;
import sun.security.action.GetLongAction;

public class SanPhamDAO extends AbstractDAO<SanPham, String> {

    @Override
    protected Class getPOJOClass() {
        return SanPham.class;
    }

    public ArrayList<SanPham> getSanPhamDuocNhieuNguoiQuanTam(int size) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s order by s.luotXem desc";
        Query query = session.createQuery(hql);
        query.setMaxResults(size);
        ArrayList<SanPham> ds = (ArrayList<SanPham>) query.list();
        return ds;
    }

    public ArrayList<SanPham> getSanPhamKhuyenMai(int size) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s order by s.luotXem desc";
        Query query = session.createQuery(hql);
        query.setMaxResults(6);
        ArrayList<SanPham> ds = (ArrayList<SanPham>) query.list();
        return ds;
    }

    public ArrayList<SanPham> getSanPhamMoiNhat(int size) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s order by s.ngayTao desc";
        Query query = session.createQuery(hql);
        query.setMaxResults(size);
        ArrayList<SanPham> ds = (ArrayList<SanPham>) query.list();
        return ds;
    }

    public static int tinhsotrang(String tukhoa, int soLuongSanPhamTrenTrang) {
        ArrayList<SanPham> ds = new ArrayList<SanPham>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s where s.tenSanPham like :ten_san_pham";
        Query query = session.createQuery(hql);
        query.setString("ten_san_pham", "%" + tukhoa + "%");
        ds = (ArrayList<SanPham>) query.list();
        session.close();
        int n = ds.size();
        int s;
        if (soLuongSanPhamTrenTrang != -1) {
            s = n / soLuongSanPhamTrenTrang;
            if (n % soLuongSanPhamTrenTrang != 0) {
                s++;
            }
        } else {
            s = 1;
        }
        return s;
    }

    public static ArrayList<SanPham> timKiemNhanh(String tukhoa, int soLuongSanPhamTrenTrang, int trang) {
        int n = (trang - 1) * soLuongSanPhamTrenTrang;
        int m = soLuongSanPhamTrenTrang;
        ArrayList<SanPham> ds = new ArrayList<SanPham>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s where s.tenSanPham like :ten_san_pham";
        Query query = session.createQuery(hql);
        query.setString("ten_san_pham", "%" + tukhoa + "%");
        if (soLuongSanPhamTrenTrang != -1) {
            query.setFirstResult(n);
            query.setMaxResults(m);
        }
        ds = (ArrayList<SanPham>) query.list();
        session.close();
        return ds;
    }

    public static int tinhSoTrangSanPhamTheoMCTDM(String MaChiTietDanhMuc, int soLuongSanPhamTrenTrang) {
         ArrayList<SanPham> ds = new ArrayList<SanPham>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s where s.chiTietDanhMuc.maChiTietDanhMuc=:ma_chi_tiet_danh_muc";
        Query query = session.createQuery(hql);
        query.setString("ma_chi_tiet_danh_muc", MaChiTietDanhMuc);
        ds = (ArrayList<SanPham>) query.list();
        session.close();
        int n = ds.size();
        int s;
        if (soLuongSanPhamTrenTrang != -1) {
            s = n / soLuongSanPhamTrenTrang;
            if (n % soLuongSanPhamTrenTrang != 0) {
                s++;
            }
        } else {
            s = 1;
        }
        return s;
    }
     public static int tinhSoTrangSanPhamTheoMDM(String MaDanhMuc, int soLuongSanPhamTrenTrang) {
         ArrayList<SanPham> ds = new ArrayList<SanPham>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s where s.chiTietDanhMuc.danhMuc.maDanhMuc=:ma_danh_muc";
        Query query = session.createQuery(hql);
        query.setString("ma_danh_muc", MaDanhMuc);
        ds = (ArrayList<SanPham>) query.list();
        session.close();
        int n = ds.size();
        int s;
        if (soLuongSanPhamTrenTrang != -1) {
            s = n / soLuongSanPhamTrenTrang;
            if (n % soLuongSanPhamTrenTrang != 0) {
                s++;
            }
        } else {
            s = 1;
        }
        return s;
    }
     public static int tinhSoTrangSanPhamAll(int soLuongSanPhamTrenTrang) {
         ArrayList<SanPham> ds = new ArrayList<SanPham>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s ";
        Query query = session.createQuery(hql);
        ds = (ArrayList<SanPham>) query.list();
        session.close();
        int n = ds.size();
        int s;
        if (soLuongSanPhamTrenTrang != -1) {
            s = n / soLuongSanPhamTrenTrang;
            if (n % soLuongSanPhamTrenTrang != 0) {
                s++;
            }
        } else {
            s = 1;
        }
        return s;
    }
    public static ArrayList<SanPham> laySanPhamTheoMCTDM(String MaChiTietDanhMuc, int soLuongSanPhamTrenTrang, int trang) {
        int n = (trang - 1) * soLuongSanPhamTrenTrang;
        int m = soLuongSanPhamTrenTrang;
        ArrayList<SanPham> ds =new ArrayList<SanPham>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s where s.chiTietDanhMuc.maChiTietDanhMuc=:ma_chi_tiet_danh_muc";
        Query query = session.createQuery(hql);
        query.setString("ma_chi_tiet_danh_muc", MaChiTietDanhMuc);
        if (soLuongSanPhamTrenTrang != -1) {
            query.setFirstResult(n);
            query.setMaxResults(m);
        }
        ds = (ArrayList<SanPham>) query.list();
        session.close();
        return ds;
    }
    public static ArrayList<SanPham> laySanPhamTheoMDM(String MaDanhMuc, int soLuongSanPhamTrenTrang, int trang) {
        int n = (trang - 1) * soLuongSanPhamTrenTrang;
        int m = soLuongSanPhamTrenTrang;
        ArrayList<SanPham> ds = new ArrayList<SanPham>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s where s.chiTietDanhMuc.danhMuc.maDanhMuc=:ma_danh_muc";
        Query query = session.createQuery(hql);
        query.setString("ma_danh_muc", MaDanhMuc);
        if (soLuongSanPhamTrenTrang != -1) {
            query.setFirstResult(n);
            query.setMaxResults(m);
        }
        ds = (ArrayList<SanPham>) query.list();
        session.close();
        return ds;
    }
    public static ArrayList<SanPham> laySanPhamAll( int soLuongSanPhamTrenTrang, int trang) {
        int n = (trang - 1) * soLuongSanPhamTrenTrang;
        int m = soLuongSanPhamTrenTrang;
        ArrayList<SanPham> ds = new ArrayList<SanPham>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from SanPham s ";
        Query query = session.createQuery(hql);
        if (soLuongSanPhamTrenTrang != -1) {
            query.setFirstResult(n);
            query.setMaxResults(m);
        }
        ds = (ArrayList<SanPham>) query.list();
        session.close();
        return ds;
    }
}
